package com.etb.wisdom.eframe.org.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.etb.core.utils.JsonUtil;
import com.etb.web.action.ETBAction;
import com.etb.web.eui.utils.EuiUtil;
import com.etb.web.utils.WebUtil;
import com.etb.wisdom.eframe.auth.AuthConstant.AuthType;
import com.etb.wisdom.eframe.auth.AuthConstant.PartyType;
import com.etb.wisdom.eframe.model.OrgOrganization;
import com.etb.wisdom.eframe.org.service.OrgOrganizationService;

@Action("org")
@Namespace("/eframe/org/organization")
public class OrgOrganizationAction extends ETBAction<OrgOrganization> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6015049741805708242L;

	@Autowired
	public OrgOrganizationService orgService;

	public void getTreedata() {
		String id = request.getParameter("id");
		String nodeType = request.getParameter("nodeType");
		WebUtil.render(JsonUtil.toJSONArray(orgService.getTreenode(id, nodeType)).toString());
	}
	
	public String addView() {
		request.setAttribute("orglevel", request.getParameter("orglevel"));
		String uplevelid = request.getParameter("orgid");
		if(StringUtils.isNotBlank(uplevelid)) {
			OrgOrganization uplevelOrg = new OrgOrganization();
			uplevelOrg.setId(uplevelid);
			service.extend(uplevelOrg);
			request.setAttribute("uplevelOrg", uplevelOrg);
		}
		return ADD;
	}
	
	@Override
	public String updateView() {
		super.updateView();
		if(dataObject.getUplevelid() != null) {
			OrgOrganization uplevelOrg = new OrgOrganization();
			uplevelOrg.setId(dataObject.getUplevelid());
			service.extend(uplevelOrg);
			request.setAttribute("uplevelOrg", uplevelOrg);
		}
		return UPDATE;
	}

	@Override
	public void save() {
		orgService.save(dataObject);
	}
	
	public void checkUniqueCode() {
		String result = PASS;
		String orgcode = request.getParameter("value");
		OrgOrganization org = new OrgOrganization();
		org.setOrgcode(orgcode);
		if(service.count(org) > 0) {
			if(id != null) {
				OrgOrganization original = new OrgOrganization();
				original.setId(id);
				service.extend(original);
				if(!original.getOrgcode().equals(orgcode)) {
					result = UNPASS;
				}
			} else {
				result = UNPASS;
			}
		}
		WebUtil.render(result);
	}
	
	public void getUnauthList() {
		String orgid = request.getParameter("orgid");
		Map<String, Object> hqlParams = new HashMap<String, Object>();
		hqlParams.put("partyid", orgid);
		hqlParams.put("partytype", PartyType.organization.toString());
		hqlParams.put("authtype", AuthType.role.toString());
		List list = service.queryByNamedsql("query.eframe.framework.auth.getUnauthList", hqlParams);
		EuiUtil.toGrid(list, null);
	}
	
	public void getAuthList() {
		String orgid = request.getParameter("orgid");
		Map<String, Object> hqlParams = new HashMap<String, Object>();
		hqlParams.put("partyid", orgid);
		hqlParams.put("partytype", PartyType.organization.toString());
		hqlParams.put("authtype", AuthType.role.toString());
		List list = service.queryByNamedsql("query.eframe.framework.auth.getAuthList", hqlParams);
		EuiUtil.toGrid(list, null);
	}
	
	public void saveRole() {
		String addRoleIds = request.getParameter("addRoleIds");
		String delRoleIds = request.getParameter("delRoleIds");
		String orgId = request.getParameter("orgid");
		orgService.saveRole(orgId, addRoleIds, delRoleIds);
	}
	
}